package com.wondream.myframework.juc;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author wondream
 * @version 1.0
 * @create 2022/11/13 10:15 AM
 */
public class AtomicLongTest {

    public static void testAtomicLong(final int threadCount, final int times)
        throws Exception{
        CountDownLatch countDownLatch = new CountDownLatch(threadCount);
        AtomicLong longAdder = new AtomicLong();
        for (int i = 0; i < threadCount; i++) {
            new Thread(() -> {
                for (int j = 0; j < times; j++) {
                    longAdder.incrementAndGet(); //是原子操作，多线程安全。
                }
                countDownLatch.countDown();
            }).start();
        }
        countDownLatch.await();
    }

    public static void main(String[] args) throws Exception {
        long start = System.currentTimeMillis();
        AtomicLongTest.testAtomicLong(10, 100000);
        long end = System.currentTimeMillis();
        System.out.println("AtomicLong运行时间："+(end-start));
    }

}
